﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Nage.Algorithms;
using Nage.Algorithms.Population;
using Nage.Algorithms.Solution;

namespace Nage.Algorithms
{
    public class GAWrapper<TSol> : IGAWrapper where TSol : ISolution
    {
        public IGeneticAlgorithm<TSol> Algorithm { get; set; }

        #region Solve
        public void Solve()
        {
            Algorithm.StepCompleted += RastriginProblemStepCompleted;
            PrintBestFit(Algorithm.Solve());
        } 
        #endregion

        #region PrintBestFit
        private void PrintBestFit(IPopulation<TSol> finalPopulation)
        {
            TSol bestFit = finalPopulation.OrderBy(x => x.Fitness).FirstOrDefault();
            Console.WriteLine("===============================================");
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("Best fit: {0}", bestFit);
            Console.ResetColor();
        }
        #endregion

        #region RastriginProblemStepCompleted
        private void RastriginProblemStepCompleted(IPopulation<TSol> currentPopulation)
        {
            Console.WriteLine("===============================================");
            Console.WriteLine(currentPopulation);
        }
        #endregion    
    }
}
